package cn.com.sparkle.paxos.client;

import java.util.concurrent.CountDownLatch;

import cn.com.sparkle.paxos.net.NetNode;

public class ConnectConfig {
	private String address;
	private volatile boolean isAutoReConnect;
	private boolean isKeepMaster;
	private CountDownLatch count = new CountDownLatch(1);
	private ConnectEvent connectEvent;

	public static interface ConnectEvent {
		public void connect(String address, NetNode node);

		public void disconnect(String address, NetNode node);
	}

	public ConnectConfig(String address, boolean isAutoReConnect,
			boolean isKeepMaster, ConnectEvent connectEvent) {
		super();
		this.address = address;
		this.isAutoReConnect = isAutoReConnect;
		this.isKeepMaster = isKeepMaster;
		this.connectEvent = connectEvent;
	}

	public boolean isKeepMaster() {
		return isKeepMaster;
	}

	public void setKeepMaster(boolean isKeepMaster) {
		this.isKeepMaster = isKeepMaster;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public boolean isAutoReConnect() {
		return isAutoReConnect;
	}

	public void setAutoReConnect(boolean isAutoReConnect) {
		this.isAutoReConnect = isAutoReConnect;
	}

	public void connected(NetNode node) {
		connectEvent.connect(address, node);
		if (count.getCount() == 1) {
			count.countDown();
		}
	}

	public void disconnected(NetNode node) {
		connectEvent.disconnect(address, node);
	}

	public void waitOnOpenEvent() throws InterruptedException {
		count.await();
	}
}
